package com.fuyao.cloud.personnel.problem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.api.feign.RemoteUserInfoService;
import com.fuyao.cloud.admin.api.utils.NumberUtils;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.datascope.DataScope;
import com.fuyao.cloud.common.excel.vo.ErrorMessage;
import com.fuyao.cloud.personnel.api.constant.CommonConstants;
import com.fuyao.cloud.personnel.api.constant.SupervisedConstants;
import com.fuyao.cloud.personnel.api.entity.Supervised;
import com.fuyao.cloud.personnel.api.vo.excel.SupervisedExcelVO;
import com.fuyao.cloud.personnel.problem.mapper.SupervisedMapper;
import com.fuyao.cloud.personnel.problem.service.SupervisedService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 监督情况表
 *
 * @author hyc
 * @date 2022-05-20 10:52:19
 */
@Service
@AllArgsConstructor
public class SupervisedServiceImpl extends ServiceImpl<SupervisedMapper, Supervised> implements SupervisedService {

    private final RemoteUserInfoService userInfoService;

    @Override
    public <E extends IPage<Supervised>> E page(E page, Wrapper<Supervised> queryWrapper) {
        return this.baseMapper.selectPageByScope(page,queryWrapper,new DataScope());
    }

    @Override
    public boolean save(Supervised entity) {
        // 默认
        Supervised tenderSupervision = this.baseMapper.selectOne(Wrappers.<Supervised>lambdaQuery().orderByDesc(Supervised::getId).last("LIMIT 1"));
        if (tenderSupervision!=null) {
            entity.setNumber(SupervisedConstants.SUPERVISED_NUMBER_PREFIX+ DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN)+NumberUtils.getIncrementNum(tenderSupervision.getNumber().substring(10)));
        }else {
            String onlyNum = SupervisedConstants.SUPERVISED_NUMBER_PREFIX+ DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN)+ CommonConstants.DEFAULT_NUMBER_SUFFIX;
            entity.setNumber(onlyNum);
        }
        return super.save(entity);
    }

    @Override
    public R importSupervised(List<SupervisedExcelVO> excelVOList, BindingResult bindingResult) {
        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();

        for (int i = 0; i < excelVOList.size(); i++) {
            SupervisedExcelVO supervisedExcelVO = excelVOList.get(i);
            Set<String> errorMsg = new HashSet<>();

            // 数据合法情况
            if (CollUtil.isEmpty(errorMsg)) {
                insertExcelSupervised(supervisedExcelVO);
            }
            else {
                // 数据不合法情况
                errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
            }
        }

        if (CollUtil.isNotEmpty(errorMessageList)) {
            return R.failed(errorMessageList);
        }

        return R.ok();
    }

    private void insertExcelSupervised(SupervisedExcelVO supervisedExcelVO) {
        Supervised supervised =new Supervised();
        BeanUtils.copyProperties(supervised,supervisedExcelVO);
        super.save(supervised);
    }
}
